<?php
class Install extends Controller {

	function Install()
	{
		parent::Controller();
		$this->load->helper('file');
	}

	function index()
	{
		echo "You should only use this installer one time. If you run it multiple times, all your discussions, todos, etc. will be erased. <a href='http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"] . "/step/1'>Start</a>";
	}
	function step()
	{
		print<<<ENDSTYLE
		<style type="text/css">
		* {
		margin: 0;
		padding: 0;
		}
		div.en {
			width: 380px !important;
			position: absolute;
			background-color: #F4FFE8;
			padding: 20px 10px;
			border: 1px solid #E4E8C8;
			left: 50%;
			top: 175px;
			margin-left: -170px;
			font-family: arial;
			font-size: 13px;
		}
		div.error {
			background-color: #FBE6F2;
			padding: 5px;
			margin-bottom: 10px;
			color: #000;
			border: 1px solid #D893A1;
		}
		form {
			width: 380px !important;
			position: absolute;
			background-color: #F4FFE8;
			padding: 20px 0;
			border: 1px solid #E4E8C8;
			left: 50%;
			top: 175px;
			margin-left: -170px;
			font-family: arial;
		}
		form li label { 
			width: 100px;
			float: left;
			clear: both;
			text-align: right;
			margin-right: 10px;
			font-size: 11px;
			position: relative;
			top: 6px;
		}
		form li {
			border-bottom: 1px solid #eff5e1;
			list-style-type: none;
			clear:both;
			padding: 6px 0 6px 0;
		}
		 form li input{
			width: 250px;
			padding: 5px 0 5px 4px;
			color: #382119;
			border: 1px solid #A0AD79;
			background-color: #eaf5d0;
			font-family: arial;
		}
		form li input.submit {
			background-color: #ecf5d5;
			border: 1px solid #bdd678;
			width: auto;
			margin-left: 110px;
			padding-right: 4px;
		}
	</style>
ENDSTYLE;

if($this->uri->segment(3) == "1"){
		if(is_writable('system/application/config/config.php') && is_writable('system/application/config/database.php') && is_writable('system/application/config/autoload.php')) {
			$ourl = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
			$nwurl =  str_replace("index.php/install/step/1","index.php/install/step/2",$ourl);
			echo '<script type="text/javascript">
window.location = "' . $nwurl . '"
</script>
';
		}
		
		else { 
					print<<<ENDHTML
		<div class="en">
		<div class="error">Error! Some of your files and directories aren't writable.</div>
		<p>CHMOD config.php, database.php, and autoload.php in system/application/config as well as the directories files and designs in the root to 777, then reload.</p>
		</div>
ENDHTML;
	}

}
if($this->uri->segment(3) == "2"){
					$url = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
					$nurl =  str_replace("index.php/install/step/2","",$url);
			$data = "\n \$config['base_url'] = '" . $nurl . "';";
				if ( ! write_file('system/application/config/config.php', $data, 'a'))
				{
					 echo 'An error has occurrred. You will probably have to install manually (don\'t worry, it only takes about 3 minutes!)';
				}
				else
				{
				print<<<ENDHTML
			<form action="../../install/step/3" method="POST">
			<ul>
			<li><label>Database host</label><input value="localhost" type="text" name="host" /></li>
			<li><label>Database user</label><input type="text" name="user" /></li>
			<li><label>Database password</label><input type="password" name="password" /></li>
			<li><label>Database name</label><input type="text" name="database" /></li>
			<li><input type="submit" class="submit" value="Next Step &raquo;" name="submitdb" /></li>
			</ul>
			</form>
ENDHTML;
				}
}
elseif($this->uri->segment(3) == "3"){
			if($_POST['submitdb']) {
				$data = "\n \$db['default']['hostname'] = '" . $_POST['host'] . "';\n \$db['default']['username'] = '" . $_POST['user'] . "';\n \$db['default']['password'] = '" . $_POST['password'] . "';\n \$db['default']['database'] = '" . $_POST['database'] . "';";
					if ( ! write_file('system/application/config/database.php', $data, 'a'))
					{
						 echo 'Unable to write the file';
					}
				}
				$this->load->database();
				$this->load->library('encrypt');
					mysql_query("
CREATE TABLE ci_sessions (
  session_id varchar(40) NOT NULL default '0',
  ip_address varchar(16) NOT NULL default '0',
  user_agent varchar(50) NOT NULL,
  last_activity int(10) unsigned NOT NULL default '0',
  session_data text NOT NULL,
  PRIMARY KEY  (session_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
");
mysql_query("CREATE TABLE comments (
  commentid int(11) NOT NULL auto_increment,
  design int(11) NOT NULL,
  `user` int(11) NOT NULL,
  `date` int(11) NOT NULL,
  body text NOT NULL,
  version int(11) NOT NULL,
  thread int(11) NOT NULL,
  PRIMARY KEY  (commentid)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");

mysql_query("CREATE TABLE designs (
  designid int(11) NOT NULL auto_increment,
  threadid int(11) NOT NULL,
  image varchar(255) NOT NULL,
  `user` int(11) NOT NULL,
  ddate int(11) NOT NULL,
  PRIMARY KEY  (designid)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");

mysql_query("CREATE TABLE discussions (
  id int(11) NOT NULL auto_increment,
  title varchar(255) NOT NULL,
  topic text NOT NULL,
  tdate int(11) NOT NULL,
  tuser int(11) NOT NULL,
  PRIMARY KEY  (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");

mysql_query("CREATE TABLE posts (
  postid int(11) NOT NULL auto_increment,
  pdate int(11) NOT NULL,
  `user` int(11) NOT NULL,
  discussion varchar(11) NOT NULL,
  body text NOT NULL,
  PRIMARY KEY  (postid)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");

mysql_query("CREATE TABLE project (
  title varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;");

mysql_query("CREATE TABLE threads (
  id int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY  (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");

mysql_query("CREATE TABLE todos (
  todo int(11) NOT NULL auto_increment,
  description varchar(255) NOT NULL,
  bydate int(11) NOT NULL,
  assigndate int(11) NOT NULL,
  assignto int(11) NOT NULL,
  completed int(1) NOT NULL,
  completedate int(11) NOT NULL,
  PRIMARY KEY  (todo)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");

mysql_query("CREATE TABLE users (
  id int(11) NOT NULL auto_increment,
  username varchar(25) NOT NULL default '',
  email varchar(100) NOT NULL default '',
  fname varchar(25) NOT NULL default '',
  lname varchar(25) NOT NULL default '',
  addr varchar(255) NOT NULL default '',
  city varchar(25) NOT NULL default '',
  state varchar(25) NOT NULL default '',
  country varchar(25) NOT NULL default '',
  zip int(11) NOT NULL default '0',
  timezone int(11) NOT NULL default '0',
  isadmin tinyint(1) NOT NULL default '0',
  `password` varchar(255) NOT NULL default '',
  PRIMARY KEY  (id)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
");
								print<<<ENDHTML
			<form action="" method="POST">
<ul>
<li><label>Username</label><input type="text" name="username" /></li>
<li><label>Name</label><input type="text" name="fname" /></li>
<li><label>Email</label><input type="text" name="email" /></li>
<li><label>Password</label><input type="password" name="pass" /></li>
<li><label>Password Again</label><input type="password" name="pass2" /></li>
<li><input class="submit" type="submit" value="Add User" /></li>
</ul>
</form>
ENDHTML;
					if($_POST['username']) {
						$auto = "\n \$autoload['libraries'] = array('database', 'Users', 'DB_Session');";
					if ( ! write_file('system/application/config/autoload.php', $auto, 'a'))
					{
						echo 'Unable to write the file';
					}
					else
					{
					}
				$this->db->set('username', $_POST['username']);
				$this->db->set('fname', $_POST['fname']);
				$this->db->set('email', $_POST['email']);
				$this->db->set('password', $this->encrypt->encode($_POST['pass']));
				$this->db->set('isadmin', '1');
				$this->db->insert('users');
				$this->db->set('title', 'designager');
				$this->db->insert('project');
				redirect('people');
				}
				
}
	}
}
